package client.domain.ui.centerPanel;

import static client.domain.SandboxMainKeys.*;
import client.internationalization.SandboxConstants;
import client.internationalization.SandboxMessages;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.maps.client.InfoWindow;
import com.google.gwt.maps.client.InfoWindowContent;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.Maps;
import com.google.gwt.maps.client.control.LargeMapControl;
import com.google.gwt.maps.client.control.MapTypeControl;
import com.google.gwt.maps.client.event.MarkerMouseOutHandler;
import com.google.gwt.maps.client.event.MarkerMouseOverHandler;
import com.google.gwt.maps.client.geocode.Geocoder;
import com.google.gwt.maps.client.geocode.LocationCallback;
import com.google.gwt.maps.client.geocode.Placemark;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.gwtext.client.widgets.Panel;

/**
 * User: nbarrios
 * Date: Nov 9, 2009
 */
public class MapUI extends Panel{

	//	Internationalization Strings
	private SandboxConstants constants = GWT.create(SandboxConstants.class);
	private SandboxMessages messages = GWT.create(SandboxMessages.class);

	private MapWidget map;
	private Geocoder geocoder = new Geocoder();

	InfoWindowContent kaitzenInfoWindowContent = new InfoWindowContent(messages.KaitzenMapInfoWindow());

	public MapUI() {
		super();
		this.setTitle(constants.maps());
		this.setClosable(true);

//		Validatations about libraries
		if (!Maps.isLoaded()) {
			Window.alert(messages.apiNotInstalled());
			return;
		}

		if (!Maps.isBrowserCompatible()) {
			Window.alert(messages.browserIsNotCompatible());
			return;
		}


		// Open a map centered on the intersection of Greenwich Meridian and Ecuator
		LatLng initialPosition = LatLng.newInstance(KAITZEN_LAT+20, KAITZEN_LNG);

		map = new MapWidget(initialPosition, DEFAULT_ZOOM_LEVEL);
		map.setSize(CENTER_PANEL_WIDTH_ST, CENTER_PANEL_TABS_HEIGH_ST);

		//	Add some controls for the map widget
		map.addControl(new LargeMapControl());	//	The zoom level. You have to comment one of these lines.
//		map.addControl(new SmallMapControl());	//	The zoom level. You have to comment one of these lines.
		map.setScrollWheelZoomEnabled(true);	//	Modify the zoom level by scrolling the wheel of your mouse.
		map.addControl(new MapTypeControl());	//	The map type (Map, Satellite, Hibryd)


//		Add a Kaitzen Marker with his own InfoWindow
		Marker kaitzenMarker  = new Marker(KAITZEN_POSITION);
		kaitzenMarker.addMarkerMouseOverHandler(new MarkerMouseOverHandler(){
			public void onMouseOver(MarkerMouseOverEvent event) {
				map.getInfoWindow().open(KAITZEN_POSITION, kaitzenInfoWindowContent);		
			}
		});
		kaitzenMarker.addMarkerMouseOutHandler(new MarkerMouseOutHandler(){
			public void onMouseOut(MarkerMouseOutEvent event) {
				map.getInfoWindow().close();
			}
		});


		map.addOverlay(kaitzenMarker);
		map.getInfoWindow().open(KAITZEN_POSITION, kaitzenInfoWindowContent);//	Add an info window to highlight our location


		//	Add the search capacity
		final FormPanel form = new FormPanel();
		form.setAction("#");
		com.google.gwt.user.client.ui.Panel formElements = new FlowPanel();
		Label label = new Label(constants.searchForAPlace());
		formElements.add(label);
		final TextBox addressBox = new TextBox();
		addressBox.setVisibleLength(40);
		formElements.add(addressBox);
		Button submit = new Button(constants.search());
		submit.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				form.submit();
			}
		});
		formElements.add(submit);
		form.add(formElements);

		form.addFormHandler(new FormHandler() {
      		public void onSubmit(FormSubmitEvent event) {
        		showAddress(addressBox.getText());
        		event.setCancelled(true);
      		}

      		public void onSubmitComplete(FormSubmitCompleteEvent event) {
      		}
    	});

		//	Add the map to this panel
		this.add(form);
		this.add(map);
	}

	private void showAddress(final String address) {

    	final InfoWindow info = map.getInfoWindow();
		geocoder.getLocations(address, new LocationCallback() {
      		public void onFailure(int statusCode) {
        		Window.alert(messages.weAreUnableToGeocodeThatPlace());
      		}

      		public void onSuccess(JsArray<Placemark> locations) {
        		final Placemark place = locations.get(0);
				final InfoWindowContent newInfoWindowContent = new InfoWindowContent(place.getAddress() + "<br>" + "<b>"+constants.contryCode()+"</b> " + place.getCountry());
				Marker newMarker  = new Marker(place.getPoint());

				newMarker.addMarkerMouseOverHandler(new MarkerMouseOverHandler(){
					public void onMouseOver(MarkerMouseOverEvent event) {
						map.getInfoWindow().open(place.getPoint(), newInfoWindowContent);		
					}
				});
				newMarker.addMarkerMouseOutHandler(new MarkerMouseOutHandler(){
					public void onMouseOut(MarkerMouseOutEvent event) {
						map.getInfoWindow().close();
					}
				});

        		map.addOverlay(newMarker);
				info.open(newMarker, newInfoWindowContent);
      		}
    	});
  	}




//	 public void onApiLoad() {
//    SearchControlOptions options = new SearchControlOptions();
//
//    // We can use custom subclasses
//    options.add(new GoogleCodeWebSearch(), ExpandMode.OPEN);
//
//    // Or configure inline
//    WebSearch ws = new WebSearch();
//    ws.setSiteRestriction("ajaxian.com");
//    ws.setUserDefinedLabel("Ajaxian");
//    ws.setResultSetSize(ResultSetSize.SMALL);
//    options.add(ws);
//    options.add(new NewsSearch());
//    options.add(new VideoSearch(), ExpandMode.CLOSED);
//    options.add(new ImageSearch());
//    options.setKeepLabel(KeepLabel.SAVE);
//    options.setLinkTarget(LinkTarget.BLANK);
//
//    SearchControl searchControl = new SearchControl(options);
//    searchControl.addKeepHandler(this);
//    searchControl.addSearchCompleteHandler(this);
//    searchControl.addSearchStartingHandler(this);
//    searchControl.execute("Google Web Toolkit");
//    hp.add(searchControl);
//  }
	
}//End Class

